package xyz.honeysuccess;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * @author Honey
 * @date 2021/12/23
 */
public class DownloadFile {

    private static final String url = "http://localhost:8080";

    public static void main(String[] args) {
        // uuid 为testFileUpLoad返回的值
        String uuid = "7cc0f01f-d22d-4dc7-b89e-03b4801a3b15";
        String result = downloadFile(url + "/file/download/" + uuid);
        System.out.println(result);
    }

    public static String downloadFile(String actionUrl){
        HttpURLConnection connection = null;
        String data = null;
        InputStream inputStream = null;
        try{
            URL url = new URL(actionUrl);
            // 打开连接
            connection = (HttpURLConnection) url.openConnection();
            // 设置请求方式：get请求
            connection.setRequestMethod("GET");
            // 发送请求
            connection.connect();
            // 获得返回的状态码
            int responseCode = connection.getResponseCode();
            // 通过connection获取输入流数据
            if (responseCode == 200) {
                inputStream = connection.getInputStream();
                data = Util.inToString(inputStream);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            Util.close(inputStream);
            if (connection != null) {
                connection.disconnect();
            }
        }
        return data == null ? "" : data;
    }




}
